package com.ccys.cms.controller.inventory;


import com.ccys.cms.service.inventory.InventoryFeedService;
import com.ccys.cms.service.productAndService.ProductService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.inventory.InventoryFeedDto;
import com.ccys.db.entity.TblInventoryFeed;
import com.ccys.db.vo.web.inventory.InventoryFeedVo;
import com.ccys.db.vo.web.inventory.ProductInfoFeedVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/inventory")
@Api(tags = "库存调整")
public class InventoryFeedController {

    @Resource
    private InventoryFeedService feedService;

    @Resource
    private ProductService productService;


    //  根据批次编号查到产品相关信息
    @ApiOperation("根据批次编号查到产品相关信息")
    @GetMapping("getProductInfoByBatchNo")
    public ResponseResult<ProductInfoFeedVo, Object> getProductInfoByBatchNo(String batchNo) {

        ProductInfoFeedVo res = feedService.getProductInfoByBatchNo(batchNo);
        return ResponseResult.success(res);
    }


    @ApiOperation("新增")
    @PostMapping("inventoryFeed")
    @Transactional
    public ResponseResult save(TblInventoryFeed model) {

        int i = feedService.insertOrUpdate(model);
        //修改产品库存
        productService.updateStockQuantityByAdjust(model);
        return ResponseResult.update(i);
    }

    @ApiOperation("查询分页")
    @GetMapping("inventoryFeed")
    public ResponseResult<PageInfo<InventoryFeedVo>, Object> getPage(InventoryFeedDto dto) {

        return feedService.getPage(dto);
    }
}
